package com.sugon.modules.sparepart.service.impl;

import cn.hutool.core.util.StrUtil;
import com.sugon.entity.SparePartSafeStockEntity;
import com.sugon.modules.base.consts.SysApproveStatusConst;
import com.sugon.modules.base.model.dto.HandleApproveResult;
import com.sugon.modules.base.service.impl.AbstractErmsBaseApproveManager;
import com.sugon.modules.sparepart.consts.SparePartSafeStockState;
import com.sugon.modules.sparepart.service.ISparePartSafeStockManager;
import com.sugon.modules.sys.consts.ProcessProgressStatus;
import com.sugon.service.SparePartSafeStockService;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysProcessInfoService;
import com.sugon.utils.ProcessInfoEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author ： YuXD
 * @description： 车间备件安全库存Manager实现类
 * @since 2022-07-25
 */
@Service
public class SparePartSafeStockManagerImpl extends AbstractErmsBaseApproveManager implements ISparePartSafeStockManager {

    @Resource
    private SysProcessInfoService processInfoService;
    @Resource
    private SysMsgService msgService;
    @Resource
    private SparePartSafeStockService sparePartSafeStockService;

    /**
     * handleWorkshopApprove
     *
     * @param approveItemId 审批事项ID
     * @param approveStatus 审批状态
     * @param remark        审批备注
     * @return
     */
    @Override
    public HandleApproveResult handleWorkshopApprove(String approveItemId, Integer approveStatus, String remark) {
        HandleApproveResult handleApproveResult = new HandleApproveResult();
        SparePartSafeStockEntity safeStock = sparePartSafeStockService.queryObject(approveItemId);
        org.springframework.util.Assert.notNull(safeStock, "车间安全库存设置不存在，请确认！");
        // 校验规程进度
        org.springframework.util.Assert.isTrue(SparePartSafeStockState.DEPT_APPROVING.equals(safeStock.getState()), "只可以审核处于审核中的车间备件安全库存！");
        handleApproveResult.setProcessProgressStatus(ProcessProgressStatus.ING);
        handleApproveResult.setTitle(StrUtil.format("审核{}[{}]的安全库存设置", safeStock.getDeptName(), safeStock.getName()));
        // 如果审核通过
        if (approveStatus == SysApproveStatusConst.PASSED) {
            safeStock.setState(SparePartSafeStockState.PASSED);
            int rows = sparePartSafeStockService.update(safeStock);
            if (rows > 0) {
                handleApproveResult.setNextNodeLevel(-1);
            }
        } else if (approveStatus == SysApproveStatusConst.NOT_PASSED) {
            safeStock.setState(SparePartSafeStockState.NOT_PASSED);
            int rows = sparePartSafeStockService.update(safeStock);
            if (rows > 0) {
                handleApproveResult.setUserId(safeStock.getCreateUser());
            }
        }
        return handleApproveResult;
    }

    /**
     * 提交审核
     *
     * @param safeStock 车间备件安全库存配置
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean commitApprove(SparePartSafeStockEntity safeStock) {
        safeStock.setState(SparePartSafeStockState.DEPT_APPROVING);
        boolean flag = sparePartSafeStockService.update(safeStock) > 0;
        if (flag) {
            String safeStockId = safeStock.getId();
            // 保存流程信息
            processInfoService.saveProcessInfo(safeStockId, ProcessInfoEnum.SPA_BJAQKC, 0, null);
            // 添加备件审批事项
            msgService.addSparePartSafeStockApproveItem(safeStockId, 0);
        }
        return flag;
    }

}
